package org.yy.controller;


import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yy.service.CodeService;
import org.yy.service.MailService;

import java.util.Random;

@RestController
public class CodeController {


    @Reference(timeout = 5000)
    MailService mailServiceClient;


    @Autowired
    CodeService codeService;

    @RequestMapping("/code/create/{email}")
    public Boolean create(@PathVariable String email) {
        Random random = new Random();// 定义随机类
        String code = "";
        for (int i = 0; i < 6; i++) {
            int result = random.nextInt(9);
            code += result;
        }
        System.out.println("开始发邮件：" + email + "\ncode：" + code);
        Boolean suc = false;
        suc = mailServiceClient.sendSimpleMail(email, "Dubbo测试验证码", code);

        if (suc) {
            codeService.saveCode(email, code);
        }

        return suc;
    }


    @RequestMapping("/code/validate/{email}/{code}")
    public Integer validate(@PathVariable String email, @PathVariable String code) {
        return codeService.validate(email, code);
    }

}
